package br.com.quiz.dao;

import org.hibernate.Query;
import org.hibernate.Session;

import br.com.quiz.domain.Admin;
import br.com.quiz.util.HibernateUtil;

public class AdminDAO{
	public static Admin filtrarPorId( int admId ) {
		Session sessao = HibernateUtil.getSessionFactory().openSession();
		Admin admin = null;
		try {
			Query consulta = sessao.getNamedQuery( "Admin.filtrarPorId" );
			consulta.setInteger( "admId", admId );
			admin = (Admin) consulta.uniqueResult();
		} catch ( RuntimeException e ) {
			throw e;
		} finally {
			sessao.close();
		}

		return admin;
	}

	public static Long verificarAdmin( int pesId ) {
		Session sessao = HibernateUtil.getSessionFactory().openSession();

		try {
			String hql = "select count(p.pesId) from Pessoa p where p.pesId in (select a.pesId from Admin a where a.pesId = p.pesId) and p.pesId= " + pesId;
			Query query = sessao.createQuery( hql );
			Long quantidadesProfessoresEncontrados = (Long) query.uniqueResult();

			return quantidadesProfessoresEncontrados;

		} catch ( RuntimeException ex ) {
			throw ex;
		} finally {
			sessao.close();
		}

	}
}
